<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('format_column_as_select'))
{
	/**
	 * 将栏目分类的数组重新组织为select列表
	 * @param array $column 栏目分类数组
	 * @param int $fatherid 要设置为选中状态的栏目父类id
	 * @return string
	 */
	function format_column_as_select($column, $fatherid = '')
	{
		$split = '';
		static $n = 0;
		$split_str = '|-';
		$option = '';
		if (is_array($column) && count($column) > 0)
		{
			$m = count($column);
			$n ++;
			for($i = 0; $i < $n - 1; $i ++)
			{
				$split .= $split_str;
			}
			foreach ($column as $row)
			{
				$m --;
				if ($row ['id'] == $fatherid)
				{
					$selected = " selected='selected' ";
				}
				else
				{
					$selected = '';
				}
				$option .= "<option value='{$row['id']}' {$selected}>{$split}{$row ['columnname']}</option>";
				$result = format_column_as_select($row ['sub'], $fatherid);
				if ($m == 0)
				{
					$n --;
					$split = substr($split, 0, strlen($split) - strlen($split_str) * 1);
				}
				$option .= $result;
			}
		}
		return $option;
	}
	
	/**
	 * 将栏目分类数组重新组织为select的列表形式
	 * @param array $column 栏目分类数组
	 * @return string
	 */
	function format_column_as_select1($column)
	{
		$split = '';
		static $n = 0;
		$split_str = '|-';
		$option = '';
		if (is_array($column) && count($column) > 0)
		{
			$m = count($column);
			$n ++;
			for($i = 0; $i < $n - 1; $i ++)
			{
				$split .= $split_str;
			}
			foreach ($column as $row)
			{
				$m --;
				$option .= "<option value='{$row['id']}'>{$split}{$row ['columnname']}</option>";
				$result = format_column_as_select($row ['sub']);
				if ($m == 0)
				{
					$n --;
					$split = substr($split, 0, strlen($split) - strlen($split_str) * 1);
				}
				$option .= $result;
			}
		}
		return $option;
	}
}

if ( ! function_exists('format_column_as_table'))
{
	/**
	 * 以表格方式列出栏目分类
	 * 
	 * @param array $column 栏目分类数组
	 * @return string
	 */
	function format_column_as_table($column)
	{
		$split = '';
		static $n = 0;
		$split_str = '|-';
		$list = '';
		if (is_array($column) && count($column) > 0)
		{
			$m = count($column);
			$n ++;
			for($i = 0; $i < $n - 1; $i ++)
			{
				$split .= $split_str;
			}
			foreach ($column as $row)
			{
				$m --;
				$list .= "<tr><td align='left'>{$split}{$row ['columnname']}</td><td><a class='edit_action' href='" . base_url('column/edit/') . "/{$row ['id']}'>" . lang('com_button_edit') . "</a></td><td><input name='listid[]' type='checkbox' value='{$row ['id']}' /></td></tr>";
				if (isset($row['sub']))
				{
					$result = format_column_as_table($row ['sub']);
				}
				else
				{
					$result = '';
				}
				
				if ($m == 0)
				{
					$n --;
					$split = substr($split, 0, strlen($split) - strlen($split_str) * 1);
				}
				$list .= $result;
			}
		}
		return $list;
	}
}
